<template>
	<form-container title="外键关系配置" :width="700" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="选择外键表：" name="fkEntityName">
				<a-select
					v-model:value="formData.fkEntityName"
					:filter-option="filterOption"
					:options="tableList"
					placeholder="请选择外键表"
					show-search
					style="width: 100%"
					@select="formFieldAssign"
				>
				</a-select>
			</a-form-item>
			<a-form-item label="外键ID：" name="fkColumnId">
				<a-select
					v-model:value="formData.fkColumnId"
					:filter-option="filterOption"
					:options="columnOptions"
					placeholder="选择树ID："
					show-search
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="显示字段：" name="fkColumnName">
				<a-select
					v-model:value="formData.fkColumnName"
					:filter-option="filterOption"
					:options="columnOptions"
					placeholder="选择树ID："
					show-search
					style="width: 100%"
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</form-container>
</template>

<script setup name="fkForm">
	// 抽屉状态
	import genBasicApi from '@/api/gen/genBasicApi'
	import { required } from '@/utils/formRules'

	const visible = ref(false)
	// 定义emit事件
	const emit = defineEmits({ successful: null })
	//表单
	const formRef = ref()
	// 表单数据，也就是默认给一些数据
	const formData = ref({})
	//提交等待
	const submitLoading = ref(false)

	const tableList = ref([]) // 实体列表
	const columnOptions = ref() // 字段
	const fkInfo = ref({}) // 当前行数据
	// 表单内设置默认的值
	const formFieldAssign = (value, options) => {
		columnOptions.value = options.tableColumns.map((item) => {
			return {
				value: item['columnName'],
				label: `${item['columnName']}:${item['columnDescription']}`
			}
		})
	}

	// 下拉框回调
	const filterOption = (input, option) => {
		return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0
	}

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true //显示抽屉
		fkInfo.value = record //当前行数据
		//表单数据
		// formData.value = { requestType: 'GET', requestValue: 'id', requestLabel: 'name' }
		// 获取数据库中的所有表
		genBasicApi.basicTables({ isAll: true }).then((data) => {
			tableList.value = data.map((item) => {
				return {
					value: item['entityName'],
					tableName: item['tableName'],
					configId: item['configId'],
					label: `${item['tableDescription']}-${item['configId']}-${item['entityName']}`,
					entityName: item['entityName'],
					tableDescription: item['tableDescription'] || item['entityName'],
					tableColumns: item['columns']
				}
			})
		})
	}

	// 默认要校验的
	const formRules = {
		fkEntityName: [required('请选择外键表')],
		fkColumnId: [required('请选择外键Id')],
		fkColumnName: [required('请选择外键字段')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			fkInfo.value.fkEntityName = formData.value.fkEntityName
			fkInfo.value.fkColumnId = formData.value.fkColumnId
			fkInfo.value.fkColumnName = formData.value.fkColumnName
			submitLoading.value = false
			onClose()
		})
	}

	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>

<style scoped></style>
